package cn.breeze.product.service.impl;

import cn.breeze.basic.utils.BaiduAuditUtils;
import cn.breeze.pet.domain.PetDetail;
import cn.breeze.product.domain.Product;
import cn.breeze.product.domain.ProductDetail;
import cn.breeze.product.domain.ProductOnlineAuditLog;
import cn.breeze.product.mapper.ProductDetailMapper;
import cn.breeze.product.mapper.ProductMapper;
import cn.breeze.product.mapper.ProductOnlineAuditLogMapper;
import cn.breeze.product.service.IProductService;
import cn.breeze.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author breeze
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    
    @Autowired
    private ProductMapper productMapper;
    @Autowired ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void onsale(Long[] ids) {
        // 根据ids去查询所有数据
        List<Product> products = productMapper.patchLoadById(ids);
        List<Long> list = new ArrayList<>();
        for (Product product : products) {
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setProductId(product.getId());
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                productOnlineAuditLog.setNote("审核失败，不合规的名字，名字为：" + product.getName());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                continue;
            }
            String resources = product.getResources();
            Integer intTmp = 0;
            if (resources != null){
                String[] split = resources.split(",");
                for (String s : split) {
                    if (!BaiduAuditUtils.ImgCensorURL(s)){
                        intTmp = 1;
                        productOnlineAuditLog.setNote("审核失败，不合规的图形，图片地址为：" + s);
                        productOnlineAuditLogMapper.save(productOnlineAuditLog);
                        continue;
                    }
                }
            }
            if (intTmp == 1){
                continue;
            }
            list.add(product.getId());
        }
        productMapper.onsale(list);
    }

    /**
     * @Title: offsale
     * @Description: 商品下架
     * @Author: Mr.D
     * @Version: 1.0
     * @Date:  2022/7/16 23:57
     * @Parameters: [ids]
     * @Return void
     */
    @Override
    public void offsale(Long[] ids) {
        productMapper.offsale(ids);
    }

    @Override
    public void remove(Long id) {
        productMapper.remove(id);
        productDetailMapper.removeByProductId(id);
    }

    @Override
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.save(detail);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.update(detail);
    }

    @Override
    public void patchRemove(Long[] ids) {
        productMapper.patchRemove(ids);
        productDetailMapper.patchRemoveByProductId(ids);
    }
}
